#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME "D:/C/contacts.txt" // 可更改保存路径

// 定义联系人结构体(链表节点)
typedef struct ContactNode {
    char name[50];
    char phone[20];
    char address[100];
    struct ContactNode* next;
} Contact;

Contact* head = NULL; // 链表头指针
int count = 0;        // 联系人计数

// 创建新节点
Contact* createNode(const char* name, const char* phone, const char* address) {
    Contact* newNode = (Contact*)malloc(sizeof(Contact));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        return NULL;
    }
    strcpy(newNode->name, name);
    strcpy(newNode->phone, phone);
    strcpy(newNode->address, address);
    newNode->next = NULL;
    return newNode;
}

// 添加联系人到链表末尾
void addContact() {
    char name[50], phone[20], address[100];
    
    printf("Enter name: ");
    scanf("%s", name);
    printf("Enter phone: ");
    scanf("%s", phone);
    printf("Enter address: ");
    scanf("%s", address);
    
    Contact* newNode = createNode(name, phone, address);
    if (newNode == NULL) return;
    
    if (head == NULL) {
        head = newNode;
    } else {
        Contact* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
    count++;
    printf("Contact added successfully!\n");
}

// 显示所有联系人
void displayAll() {
    if (head == NULL) {
        printf("Contact list is empty!\n");
        return;
    }
    
    printf("\nAll Contacts (%d):\n", count);
    printf("%-20s %-15s %-30s\n", "Name", "Phone", "Address");
    
    Contact* current = head;
    while (current != NULL) {
        printf("%-20s %-15s %-30s\n", 
               current->name, 
               current->phone, 
               current->address);
        current = current->next;
    }
}

// 搜索联系人
void searchContact() {
    char name[50];
    printf("Enter name to search: ");
    scanf("%s", name);
    
    Contact* current = head;
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            printf("\nContact Found:\n");
            printf("Name: %s\nPhone: %s\nAddress: %s\n",
                   current->name,
                   current->phone,
                   current->address);
            return;
        }
        current = current->next;
    }
    printf("Contact not found!\n");
}

// 删除联系人
void deleteContact() {
    char name[50];
    printf("Enter name to delete: ");
    scanf("%s", name);
    
    Contact* current = head;
    Contact* prev = NULL;
    
    while (current != NULL) {
        if (strcmp(current->name, name) == 0) {
            if (prev == NULL) {
                // 删除头节点
                head = current->next;
            } else {
                prev->next = current->next;
            }
            free(current);
            count--;
            printf("Contact deleted successfully!\n");
            return;
        }
        prev = current;
        current = current->next;
    }
    printf("Contact not found!\n");
}

// 保存数据到文件
void saveToFile() {
    FILE* file = fopen(FILENAME, "w");
    if (file == NULL) {
        printf("Failed to save file!\n");
        return;
    }
    
    Contact* current = head;
    while (current != NULL) {
        fprintf(file, "%s %s %s\n", 
                current->name,
                current->phone,
                current->address);
        current = current->next;
    }
    fclose(file);
    printf("Data saved to file!\n");
}

// 从文件加载数据
void loadFromFile() {
    const char* filename="D:/C/contacts.txt";

    FILE* file = fopen(filename, "r");
    if (file == NULL) return;
    
    char name[50], phone[20], address[100];
    
    // 清空现有链表
    while (head != NULL) {
        Contact* temp = head;
        head = head->next;
        free(temp);
    }
    count = 0;
    
    // 读取文件并重建链表
    while (fscanf(file, "%s %s %s", name, phone, address) == 3) {
        Contact* newNode = createNode(name, phone, address);
        if (newNode == NULL) continue;
        
        if (head == NULL) {
            head = newNode;
        } else {
            Contact* current = head;
            while (current->next != NULL) {
                current = current->next;
            }
            current->next = newNode;
        }
        count++;
    }
    fclose(file);
}

// 释放链表内存
void freeList() {
    Contact* current = head;
    while (current != NULL) {
        Contact* temp = current;
        current = current->next;
        free(temp);
    }
    head = NULL;
    count = 0;
}

// 退出程序
void exitProgram() {
    char choice;
    printf("\nDo you want to save before exiting? (y/n): ");
    scanf(" %c", &choice);
    
    if (choice == 'y' || choice == 'Y') {
        saveToFile();
    }
    
    freeList();
    printf("Goodbye!\n");
    exit(0);
}

int main() {
    loadFromFile();
    
    int choice;
    do {
        printf("\nContact Management System\n");
        printf("1. Add Contact\n");
        printf("2. Delete Contact\n");
        printf("3. Show All Contacts\n");
        printf("4. Search Contact\n");
        printf("5. Save Data\n");
        printf("6. Load from file\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        
        switch(choice) {
            case 1: addContact(); break;
            case 2: deleteContact(); break;
            case 3: displayAll(); break;
            case 4: searchContact(); break;
            case 5: saveToFile(); break;
            case 6: loadFromFile();break;
            case 0: exitProgram(); break;
            default: printf("Invalid choice!\n");
        }
    } while(1); // 无限循环，通过exitProgram退出
    
    return 0;
}